package cn.yogehaoren.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setUsername("guest");

        //2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过Connection创建Channel
        Channel channel = connection.createChannel();


        for(int i=0;i<10;i++){
            // 4. 通过Channel发送数据
            // 不指定交换机时，rabbitmq会把消息自动转发到，与routeKey相同的queue中
            channel.basicPublish("", "test001", null, ("Hello RabbitMQ-"+i).getBytes());
        }


        // 5. 关闭连接
        channel.close();
        connection.close();


    }

}
